exports.CloudSmashLand = function() {
    this.round = 0;
    this.step = GlobalSmashLandRank.PERIOD_WAIT;
    this.rankPlayers = [];

    this.init = function(callback) {
        this.rank();
        callback && callback(true);
    };
    
    this.getRanks = function() {
        if (GlobalSmashLandRank.getStep() != this.step) {
            this.rank();
        }
        return this.rankPlayers;
    };

    this.rank = function() {
        this.round = GlobalSmashLandRank.getRound();
        this.step = GlobalSmashLandRank.getStep();
        this.rankPlayers = [];

        if (this.step == GlobalSmashLandRank.PERIOD_WAIT) {
            return;
        }

        var rankPlayers = [];
        gPlayers.each(function(player) {
            var smashland = player.user.smashland;
            if (smashland && smashland.score > 0 && (this.round == smashland.round)) {
                smashland.rankScore = smashland.score;
                rankPlayers.push(player);
            }
        }.bind(this));

        rankPlayers.sort(function(a, b) {
            return b.user.smashland.score - a.user.smashland.score; 
        });

        for (var rank = 1, len = rankPlayers.length; rank <= len; ++rank) {
            var player = rankPlayers[rank - 1];
            var smashland = player.user.smashland;
            if (smashland.rank != rank) {
                smashland.rank = rank;
                player.markDirty('smashland');
            }
        }

        this.rankPlayers = rankPlayers;
    }
}

exports.rank = function(player, req, resp, onHandled){
    gCloudSmashLand.rank(); 
    onHandled();
}

exports.updateScore = function(player, req, resp, onHandled){
    do {
        var score = +req.args.score;
        var smashland = player.getSmashLand();
        smashland.score = score;
        player.markDirty('smashland');
    } while(false);

    onHandled();
}

exports.get_rank = function(player, req, resp, onHandled){
    do {
        var score = +req.args.score;
        var page = +req.args.page || 0;
        var pageCount = 5;
       
        var ranks = [];
        var rankPlayers = gCloudSmashLand.getRanks();
        for (var i = page*pageCount, max = Math.min((page+1)*pageCount, rankPlayers.length); i < max; ++i) {
            var rankUser = rankPlayers[i].user;
            ranks.push({
                uid: rankUser._id,
                name: rankUser.info.un,
                rank: rankUser.smashland.rank,
                score: rankUser.smashland.rankScore,
            });
        }

        var smashland = player.getSmashLand();
        resp.data.self = smashland;
        resp.data.ranks = ranks;
    }while(false);

    onHandled();
}

exports.get_rank_award = function(player, req, resp, onHandled){
    do {
        gCloudSmashLand.getRanks();
        var smashland = player.getSmashLand();
        var rank = smashland.rank;

        if (!rank) {
            resp.code = 1; resp.desc = 'without match'; break;
        }

        if (smashland.awrad) {
            resp.code = 1; resp.desc = 'award got'; break;
        }

        var awardId = 0;
        for (var id in gConfSmashLandRankAward) {
            if (rank <= gConfSmashLandRankAward[id].Rank) {
                awardId = id;
                break;
            }
        }

        if (!awardId) {
            resp.code = 1; resp.desc = 'not in rank'; break;
        }

        smashland.award = 1;
        player.markDirty('smashland');

        resp.data.id = awardId;
    } while (false);

    onHandled();
}
